export const dateRange = (startDate, endDate, format) => {
  let dateArray = [];
  let currentDate = new Date(startDate);
  endDate = new Date(endDate);
  while (currentDate <= endDate) {
    dateArray.push(formatTimeString(currentDate, format || 'yyyy/MM/dd'));
    currentDate = dateASC(currentDate, 1);
  }
  return dateArray;
};

export const dateASC = (date, interval) => {
  date = new Date(date);

  return date.setDate(date.getDate() + interval); // 设置天数 + interval 天
};

export const dateCalc = (date, interval, calc) => {
  date = new Date(date);
  let asc = date.getDate() + interval;
  let dec = date.getDate() - interval;
  let diff = calc === 'asc' ? asc : dec
  return date.setDate(diff); // 设置天数 + interval 天
};

export const formatTimeString = (time, format) => {
  var formatTemp = format || 'yyyy-MM-dd';
  var t = new Date(time);
  var tf = function (i) {
    return (i < 10 ? '0' : '') + i;
  };
  return formatTemp.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear());
      case 'MM':
        return tf(t.getMonth() + 1);
      case 'mm':
        return tf(t.getMinutes());
      case 'dd':
        return tf(t.getDate());
      case 'HH':
        return tf(t.getHours());
      case 'ss':
        return tf(t.getSeconds());
    }
  });
};

export const monthRange = (startDate, endDate) => {
  let monthArray = [];
  let currentDate = new Date(startDate);
  endDate = new Date(endDate);
  while (currentDate <= endDate) {
    monthArray.push(formatTimeString(currentDate, 'yyyy/MM'));
    currentDate = dateASC(currentDate, 1);
  }
  monthArray = [...new Set(monthArray)];
  return monthArray;
};
